package org.store.math;

public class Stack<E> {
    private Entity<E> head;
    int count;

    public void push(E e) {
        if (e == null) {
            throw new IllegalArgumentException("cannot be null");
        }
        count++;
        head = new Entity<>(e, head);
    }

    public E pop() {
        if (head == null) {
            return null;
        }
        count--;
        E v = head.value;
        head = head.next;
        return v;
    }

    public int size() {
        return count;
    }

    public boolean isEmpty() {
        return head == null;
    }

    private static class Entity<E> {
        final E value;
        final Entity<E> next;

        private Entity(E value, Entity<E> next) {
            this.value = value;
            this.next = next;
        }
    }
}
